pip install flask flask_sqlalchemy flask_login
app.py
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import pandas as pd
import matplotlib.pyplot as plt
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['UPLOAD_FOLDER'] = 'uploads'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# 用戶模型
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
password = db.Column(db.String(150), nullable=False)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# 在应用上下文内创建数据库
with app.app_context():
db.create_all()
# 定义根路由
@app.route('/')
@login_required
def index():
return render_template('index.html')
# 定义注册路由
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if User.query.filter_by(username=username).first():
flash('Username already exists!')
return redirect(url_for('register'))
new_user = User(username=username, password=password)
db.session.add(new_user)
db.session.commit()
flash('Registration successful! Please log in.')
return redirect(url_for('login'))
return render_template('register.html')
# 定义登录路由
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and user.password == password:
login_user(user)
flash('Logged in successfully.')
return redirect(url_for('index'))
else:
flash('Invalid username or password.')
return render_template('login.html')
# 定义登出路由
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out.')
return redirect(url_for('login'))
# 启动Flask服务
if __name__ == '__main__':
app.run(debug=True)
index.html
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PTT 文章分析結果</title>
</head>
<body>
<h1>PTT 文章數據分析結果</h1>
<form action="/" method="post" enctype="multipart/form-data">
<label for="file">上傳CSV文件:</label>
<input type="file" name="file" id="file">
<button type="submit">上傳並分析</button>
</form>
{% if total_articles is not none %}
<p>文章總數: {{ total_articles }}</p>
<p>平均每篇文章的圖片數量: {{ avg_image_count }}</p>
<p>平均每篇文章的表格數量: {{ avg_table_count }}</p>
<h2>圖片數量分佈圖</h2>
<img src="/static/images/image_distribution.png" alt="圖片數量分佈圖">
<h2>表格數量分佈圖</h2>
<img src="/static/images/table_distribution.png" alt="表格數量分佈圖">
<h2>圖片數量與表格數量的關係圖</h2>
<img src="/static/images/image_table_relationship.png" alt="圖片數量與表格數量的關係圖">
{% endif %}
</body>
</html>
login.html
<!-- templates/login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="POST" action="/login">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<button type="submit">Login</button>
</form>
<p>Don't have an account? <a href="/register">Register here</a></p>
</body>
</html>
register.html
<!-- templates/register.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="POST" action="/register">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<button type="submit">Register</button>
</form>
<p>Already have an account? <a href="/login">Log in here</a></p>
</body>
</html>